//
//  SuitViewController.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/12/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "SuitViewController.h"
#import "UIHelper.h"
#import "SuitView.h"
#import "DataManager.h"
#import "ProductDetailViewControllerEx.h"
#import "TKImageCache.h"
#import "PMGlobal.h"
#import "SuitObject.h"
#import "TipView.h"
#import "PictureViewController.h"
#import "FPPopoverController.h"
#import "SuitDetailViewController.h"
@interface SuitViewController ()<UIScrollViewDelegate>{
    NSArray *suits_;
    
    NSMutableArray *suitView_;
    
    TKImageCache *imageCache_;
    
    UIButton * handoff_;
    
    NSArray *keyCategories_;
    NSMutableArray *filterBtns_;
    float cWidth;
    
    NSString *seriesName_;
}

@end

@implementation SuitViewController

//- (void)loadView{
//    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
//    [self setView:v];
//}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setNavTitle:@"重点搭配"];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suitrefresh:) name:@"suitrefresh" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchrefresh:) name:@"searchrefresh" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(totalrefresh:) name:@"totalrefresh" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suitFilterRefresh:) name:@"suitFilterRefresh" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pop:) name:@"pop" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showtip:) name:@"showtip" object:nil];
//        suits_ = [[DataManager sharedManager] getSuitArray];
        
        imageCache_ = [[TKImageCache alloc] initWithCacheDirectoryName:@"image"];
        imageCache_.notificationName = @"suit_image";
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suit_image:) name:@"suit_image" object:nil];
        
#ifdef DHH_iOS
        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[[UIHelper getInstance] getBundleImage:@"main_recommend_iphone.png"] tag:0];
        self.tabBarItem = tabBarItem;
#endif
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toDetail:) name:@"goToProductDetail2" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toDetail1:) name:@"toDetail1" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    KeyFilter = [[NSMutableArray alloc]init];
    ListImg = [[NSMutableArray alloc]init];
    keyCategories_ = [[DataManager sharedManager] getKeyCategories];
	// Do any additional setup after loading the view.

    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [[PMGlobal getInstance]getretimage];
    [b setImage:img forState:UIControlStateNormal];
    b.frame = CGRectMake(10, 10, img.size.width, img.size.height);
    [b addTarget:self action:@selector(ret) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:b];
    self.navigationItem.leftBarButtonItem = left;
    
    UIImage *bgImage = [[UIHelper getInstance] getBundleImage:@"suit_bg.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
    
    isclick = NO;
    isnoobj = NO;
    
#ifdef DHH_iOS
    
  //  float offset = 35;
    img = [[UIHelper getInstance] getBundleImage:@"iphone_tint1.png"];
    filtertint = [[UIImageView alloc]initWithImage:img];
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
    {
        filtertint.frame = CGRectMake(0, 64, img.size.width, img.size.height);
    }
    else
    {
        filtertint.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    }
    filtertint.userInteractionEnabled = YES;
    [self.view addSubview:filtertint];
    
    img = [[UIHelper getInstance] getBundleImage:@"iphone_tint.png"];
    tint = [[UIImageView alloc]initWithImage:img];
    tint.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    tint.userInteractionEnabled = YES;
    tint.top = filtertint.bottom;
    [self.view addSubview:tint];
    
    filterscroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, filtertint.width, filtertint.height)];
    filterscroll.delegate = self;
    [filtertint addSubview:filterscroll];
    
    filterBtns_ = [NSMutableArray array];
    //设置筛选条件
    for ( int i = 0; i < keyCategories_.count; i++ ) {
        
        UIButton *b = [[UIHelper getInstance] createButtonByFrame:CGRectMake(0, 0, 120, filtertint.height)];
        [b setImage:[[UIHelper getInstance] getBundleImage:@"order_next.png"] forState:UIControlStateNormal];
        [filterscroll addSubview:b];
        [b setBackgroundColor:[UIColor clearColor]];
        [b setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        b.titleLabel.font = [UIFont systemFontOfSize:14];
        b.titleLabel.adjustsFontSizeToFitWidth=YES;
        b.left = (b.width+10)*i;
        [b addTarget:self action:@selector(keyClicked:) forControlEvents:UIControlEventTouchUpInside];
        b.tag = [[keyCategories_ objectAtIndex:i] intValue]+200;
        b.imageEdgeInsets = UIEdgeInsetsMake(0, 90, 0, 0);
        [filterBtns_ addObject:b];
        
        KeyObject* firstkey = [[[DataManager sharedManager]getKeyObjects:(i+1)]objectAtIndex:0];
        [b setTitle:firstkey.keyName forState:UIControlStateNormal];
        [KeyFilter addObject:firstkey];
        CGSize s1 = [firstkey.keyName sizeWithFont:b.titleLabel.font];
        if (s1.width>98) {
            b.imageEdgeInsets = UIEdgeInsetsMake(0, 90+(98-56)/2, 0, 0);
        }
        else
        {
            b.imageEdgeInsets = UIEdgeInsetsMake(0, 90+(s1.width-56)/2, 0, 0);
        }

        filterscroll.contentSize = CGSizeMake(b.right, filtertint.height);
    }
    
    //初始化菜单
    for ( int i = 0; i < keyCategories_.count; i++ ) {
        
        [self updateKeyButton:i+1];
    }
    
    codesearch = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 150, 23)];
    codesearch.backgroundColor = [UIColor clearColor];
    codesearch.center = CGPointMake(self.view.width/2, [[UIHelper getInstance] getBundleImage:@"iphone_tint.png"].size.height/2);
    codesearch.delegate = self;
    codesearch.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    codesearch.returnKeyType = UIReturnKeySearch;
    [tint addSubview:codesearch];
    
    page = 0;
    num = 20;
    int maxCol = 2;
    float xOffset = 10;
    float yOffset = 20;
    img = [[UIHelper getInstance] getBundleImage:@"iphone_tint.png"];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, img.size.height, 320,self.view.height-tint.bottom-10)];

    }
    else
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, img.size.height, 320,self.view.height-tint.bottom-10-44)];
    }
    scroll.backgroundColor = [UIColor clearColor];
    scroll.top = tint.bottom+10;
    scroll.delegate = self;
    [self.view addSubview:scroll];
    
    float height = 0;
    
    for (int i = 0; i < num && i<suits_.count; i++) {
        //        if (style) {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial style:style page:page pageSize:20];
        //        }
        //        else
        //        {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial page:page pageSize:20];
        //        }
        
        
        SuitObject *obj = [suits_ objectAtIndex:i];
        SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0];
        // v.tag = 100+i;
        //        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pThumbURL];
        //        UIImage *img = [imageCache_ imageForKey:obj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        //        v.imageview.image = img;
        v.tag = 100+i;
        NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
        UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        if (img==nil) {
            v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];
        }
        else
        {
            v.imageview.image = img;
        }
        
        [scroll addSubview:v];
        
        int row = i / maxCol;
        int col = i % maxCol;
        
        v.left = xOffset + col * (v.width + 10);
        v.top = yOffset + row * (v.height + 10);
        
        height = v.bottom+10;
        
    }
    
    scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
    
    refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectMake(0, 0, 1024, 0)];
    refreshView.delegate = self;
    refreshView.top = scroll.bottom;
    
    [scroll addSubview:refreshView];
    [self setRefreshView];
    selectedlist = 0;
    /*
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 64+88, 280, self.view.height-70-64)];
    }
    else
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 64+88, 280, self.view.height-70-44)];
    }
        [self.view addSubview:scroll];
        scroll.backgroundColor = [UIColor clearColor];
        scroll.centerX = 160;
        scroll.top = tint.bottom;
        scroll.showsVerticalScrollIndicator = NO;
        scroll.showsHorizontalScrollIndicator = NO;
        scroll.bounces = NO;
        scroll.clipsToBounds = NO;
        scroll.delegate = self;
        [self updatePage:scroll];
        
        //  float cWidth = 0;
        scroll.contentSize = CGSizeMake(scroll.width * suits_.count, scroll.height);
        scroll.pagingEnabled = YES;
*/
    /*
     scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, 320,366)];
     [self.view addSubview:scroll];
     scroll.backgroundColor = [UIColor clearColor];
     scroll.center = CGPointMake(320/2, (460-44)/2);
     scroll.showsHorizontalScrollIndicator = NO;
     scroll.clipsToBounds = NO;
     scroll.delegate = self;
     
   //  cWidth = 920;
     
     
     suitView_ = [NSMutableArray array];
     // for (int i = 0; i < suits_.count; i++) {
     //        SuitObject *obj = [suits_ objectAtIndex:0];
     //        SuitView *s = [[SuitView alloc] initWithSuitObject:obj];
     //
     //        [suitView_ addObject:s];
     //
     //        [scroll addSubview:s];
     //        s.center = CGPointMake(scroll.width/2, scroll.height/2);
     //        cWidth = s.right;
     //}
     
     [self updatePage:scroll];
     scroll.contentSize = CGSizeMake(scroll.width * suits_.count, scroll.height);
     
     scroll.pagingEnabled = YES;
     
     selectedlist = 0;
     */
#else
     
    
    UIImage *tintImage = [[UIHelper getInstance] getBundleImage:@"detail_tint.png"];
    tintImage = [tintImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
    
    tint = [[UIImageView alloc] initWithImage:tintImage];
    tint.width = self.view.height;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        tint.top = 64;
    }
    tint.userInteractionEnabled = YES;
    [self.view addSubview:tint];
    
    img = [[UIHelper getInstance] getBundleImage:@"detail_codesearch.png"];
    UIImageView *search = [[UIImageView alloc]initWithImage:img];
    search.frame = CGRectMake(0, 0, img.size.width*4/3, img.size.height*4/3);
    search.center = CGPointMake(900, tint.height/2);
    search.tag = 1;
    [tint addSubview:search];
    
#ifdef SUIT_SHOWCASE_ENABLE
    
    
    handoff_ = [UIButton buttonWithType:UIButtonTypeCustom];
    handoff_.frame = CGRectMake(55, 10, 80, 40);
    handoff_.centerY = tint.height/2;
    [handoff_ setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
    [handoff_ setTitle:@"陈列柜" forState:UIControlStateNormal];
    [handoff_ addTarget:self action:@selector(handoff:) forControlEvents:UIControlEventTouchUpInside];
    [tint addSubview:handoff_];
    UIBarButtonItem *handoff = [[UIBarButtonItem alloc] initWithCustomView:handoff_];
    NSArray *arr = [NSArray arrayWithObject:handoff];
    self.navigationItem.rightBarButtonItems = arr;
#endif
    
    filterBtns_ = [NSMutableArray array];
    //设置筛选条件
    for ( int i = 0; i < keyCategories_.count; i++ ) {
        
        UIButton *b = [[UIHelper getInstance] createButtonByFrame:CGRectMake(0, 0, 120, tint.height)];
        [b setImage:[[UIHelper getInstance] getBundleImage:@"order_next.png"] forState:UIControlStateNormal];
        [tint addSubview:b];
        [b setBackgroundColor:[UIColor clearColor]];
        [b setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        b.titleLabel.font = [UIFont systemFontOfSize:14];
        b.titleLabel.adjustsFontSizeToFitWidth=YES;
        b.left = (b.width+10)*i;
        [b addTarget:self action:@selector(keyClicked:) forControlEvents:UIControlEventTouchUpInside];
        b.tag = [[keyCategories_ objectAtIndex:i] intValue]+200;
        b.imageEdgeInsets = UIEdgeInsetsMake(0, 90, 0, 0);
        [filterBtns_ addObject:b];
        
        KeyObject* firstkey = [[[DataManager sharedManager]getKeyObjects:(i+1)]objectAtIndex:0];
        [b setTitle:firstkey.keyName forState:UIControlStateNormal];
        CGSize s1 = [firstkey.keyName sizeWithFont:b.titleLabel.font];
        if (s1.width>98) {
            b.imageEdgeInsets = UIEdgeInsetsMake(0, 90+(98-56)/2, 0, 0);
        }
        else
        {
            b.imageEdgeInsets = UIEdgeInsetsMake(0, 90+(s1.width-56)/2, 0, 0);
        }
        [KeyFilter addObject:firstkey];
        
    }
    
    
    //初始化菜单
    for ( int i = 0; i < keyCategories_.count; i++ ) {
        
        [self updateKeyButton:i+1];
    }
    
    codesearch = [[UITextField alloc]initWithFrame:CGRectMake(863, 12, 140, 20)];
    codesearch.backgroundColor = [UIColor clearColor];
    codesearch.delegate = self;
    codesearch.tag = 1000;
    codesearch.centerY = tint.height/2;
    codesearch.placeholder = SEARCH_CODENAME;
    codesearch.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    codesearch.returnKeyType = UIReturnKeySearch;
    [tint addSubview:codesearch];
    
    suitBG = [[UIHelper getInstance] getBundleImage:@"suitView_bg.png"];
    
    page = 0;
    num = 20;
    int maxCol = 5;
    float xOffset = 50;
    float yOffset = 30;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+tint.bottom, 1024,748-44-64)];
    }
    else
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+tint.bottom, 1024,748-44-54)];
    }
    scroll.backgroundColor = [UIColor clearColor];
    scroll.delegate = self;
    [self.view addSubview:scroll];
    
    float height = 0;
    
    for (int i = 0; i < num && i<suits_.count; i++) {
        //        if (style) {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial style:style page:page pageSize:20];
        //        }
        //        else
        //        {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial page:page pageSize:20];
        //        }
        
        
        SuitObject *obj = [suits_ objectAtIndex:i];
        SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0 type:SuitView_obj];
        v.tag = 100+i;
        NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
        UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        if ([[PMGlobal getInstance]isImageEnable]) {
            if (img) {
                v.imageview.image = img;
            }else{
                v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }
        }else{
            v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }
        
        [scroll addSubview:v];
        
        int row = i / maxCol;
        int col = i % maxCol;
        
        v.left = xOffset + col * (v.width + 20);
        v.top = yOffset + row * (v.height + 20);
        
        height = v.bottom;
        
    }
    
    scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
    
    refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectMake(0, 0, 1024, 0)];
    refreshView.delegate = self;
    refreshView.top = scroll.bottom;
    
    [scroll addSubview:refreshView];
    [self setRefreshView];
    
    [self totalrefresh:nil];
    /*
    view = [[UIView alloc]initWithFrame:CGRectMake(35, 35, 60, 80)];
    view.backgroundColor = [UIColor grayColor];
    view.hidden = YES;
    [self.view addSubview:view];
     */
    /*
    float offset = 45;
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(offset, 0, 1024-offset*2,594)];
    [self.view addSubview:scroll];
    scroll.backgroundColor = [UIColor clearColor];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        scroll.center = CGPointMake(1024/2, (748+64)/2);
    }
    else
    {
        scroll.center = CGPointMake(1024/2, 748/2);
    }
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.clipsToBounds = NO;
    scroll.delegate = self;

    cWidth = 920;
 
    
    suitView_ = [NSMutableArray array];
   // for (int i = 0; i < suits_.count; i++) {
//        SuitObject *obj = [suits_ objectAtIndex:0];
//        SuitView *s = [[SuitView alloc] initWithSuitObject:obj];
//        
//        [suitView_ addObject:s];
//        
//        [scroll addSubview:s];
//        s.center = CGPointMake(scroll.width/2, scroll.height/2);
//        cWidth = s.right;
    //}
    
    [self updatePage:scroll];
    scroll.contentSize = CGSizeMake(scroll.width * suits_.count, scroll.height);
    
    scroll.pagingEnabled = YES;
    */
    selectedlist = 0;
#endif
}

-(void)viewDidAppear:(BOOL)animated{
       
    //suitView_ = [NSMutableArray array];
//    for (int i = 1; i < suits_.count; i++) {
//        SuitObject *obj = [suits_ objectAtIndex:i];
//        SuitView *s = [[SuitView alloc] initWithSuitObject:obj];
//        
//        [suitView_ addObject:s];
//        
//        [scroll addSubview:s];
//
//        cWidth = s.right;
//    }
//    scroll.contentSize = CGSizeMake(cWidth+30, scroll.height);
}

- (void)updateKeyButton:(int)selectedCategory{
    
    KeyObject* key = [KeyFilter objectAtIndex:selectedCategory-1];
    
    UIButton* btn = (UIButton*)[filterBtns_ objectAtIndex:key.categoryCode-1];
    
    [btn setTitle:key.keyName forState:UIControlStateNormal];
    
    CGSize s1 = [key.keyName sizeWithFont:btn.titleLabel.font];
    if (s1.width>98) {
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 90+(98-56)/2, 0, 0);
    }
    else
    {
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 90+(s1.width-56)/2, 0, 0);
    }
    //层层往下找，是否有连动的子级菜单
    NSArray*child = [[DataManager sharedManager]getChildrenKeyObjects:key.keyID];
    
    if (child.count>0) {
        //取第一个值作为过滤条件
        KeyObject *firstchild = [child objectAtIndex:0];
        
        [KeyFilter replaceObjectAtIndex:firstchild.categoryCode-1 withObject:firstchild];
        
        UIButton* btn = (UIButton*)[filterBtns_ objectAtIndex:firstchild.categoryCode-1];
        
        [btn setTitle:firstchild.keyName forState:UIControlStateNormal];
        
        //如果能找到子集菜单则递归调用子集
        [self updateKeyButton:firstchild.categoryCode];
    }
    
}

//统一刷新方法
-(void)newrefresh{
    [s removeFromSuperview];
    isnoobj = NO;
#ifdef DHH_iOS
    page = 0;
    num = 20;
    int maxCol = 2;
    float xOffset = 10;
    float yOffset = 20;
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"iphone_tint.png"];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, img.size.height, 320,self.view.height-tint.bottom-10)];
        
    }
    else
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, img.size.height, 320,self.view.height-tint.bottom-10)];
    }
    scroll.backgroundColor = [UIColor clearColor];
    scroll.top = tint.bottom+10;
    scroll.delegate = self;
    [self.view addSubview:scroll];
    
    float height = 0;
    
    for (int i = 0; i < num && i<suits_.count; i++) {
        //        if (style) {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial style:style page:page pageSize:20];
        //        }
        //        else
        //        {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial page:page pageSize:20];
        //        }
        
        
        SuitObject *obj = [suits_ objectAtIndex:i];
        SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0];
        // v.tag = 100+i;
        //        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pThumbURL];
        //        UIImage *img = [imageCache_ imageForKey:obj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        //        v.imageview.image = img;
        v.tag = 100+i;
        NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
        UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        if (img==nil) {
            v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];
        }
        else
        {
            v.imageview.image = img;
        }

        [scroll addSubview:v];
        
        int row = i / maxCol;
        int col = i % maxCol;
        
        v.left = xOffset + col * (v.width + 10);
        v.top = yOffset + row * (v.height + 10);
        
        height = v.bottom+10;
        
    }
    
    scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
    
    refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectMake(0, 0, 1024, 0)];
    refreshView.delegate = self;
    refreshView.top = scroll.bottom;
    
    [scroll addSubview:refreshView];
    [self setRefreshView];
#else
    page = 0;
    num = 20;
    int maxCol = 5;
    float xOffset = 50;
    float yOffset = 30;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+tint.bottom, 1024,748-44-64)];
    }
    else
    {
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+tint.bottom, 1024,748-44-54)];
    }
    scroll.backgroundColor = [UIColor clearColor];
    scroll.delegate = self;
    [self.view addSubview:scroll];
    
    float height = 0;
    
    for (int i = 0; i < num && i<suits_.count; i++) {
        //        if (style) {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial style:style page:page pageSize:20];
        //        }
        //        else
        //        {
        //            product = [[DataManager sharedManager] getProduct:nil type:pserial page:page pageSize:20];
        //        }
        
        
        SuitObject *obj = [suits_ objectAtIndex:i];
        SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0 type:SuitView_obj];
        // v.tag = 100+i;
        //        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pThumbURL];
        //        UIImage *img = [imageCache_ imageForKey:obj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        //        v.imageview.image = img;
        v.tag = 100+i;
        NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
        UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
        if ([[PMGlobal getInstance]isImageEnable]) {
            if (img) {
                v.imageview.image = img;
            }else{
                v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }
        }else{
            v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }

        [scroll addSubview:v];
        
        int row = i / maxCol;
        int col = i % maxCol;
        
        v.left = xOffset + col * (v.width + 20);
        v.top = yOffset + row * (v.height + 20);
        
        height = v.bottom;
        
    }
    
    scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
    
    refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectMake(0, 0, 1024, 0)];
    refreshView.delegate = self;
    refreshView.top = scroll.bottom;
    
    [scroll addSubview:refreshView];
    [self setRefreshView];
    /*
    view = [[UIView alloc]initWithFrame:CGRectMake(35, 35, 60, 80)];
    view.backgroundColor = [UIColor grayColor];
    view.hidden = YES;
    [self.view addSubview:view];
     */
    /*
    float offset = 45;
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(offset, 0, 1024-offset*2, suitBG.size.height)];
    [self.view addSubview:scroll];
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.backgroundColor = [UIColor clearColor];
    scroll.center = CGPointMake(1024/2, (748+64)/2);
    scroll.clipsToBounds = NO;
    scroll.delegate = self;
    */
//    float cWidth = 0;
    
//    suitView_ = [NSMutableArray array];
//    for (int i = 0; i < suits_.count; i++) {
//        SuitObject *obj = [suits_ objectAtIndex:i];
//        SuitView *s = [[SuitView alloc] initWithSuitObject:obj];
//        
//        [suitView_ addObject:s];
//        
//        [scroll addSubview:s];
//        s.center = CGPointMake(scroll.width/2 + scroll.width * i, scroll.height/2);
//        cWidth = s.right;
//    }
    /*
    [self updatePage:scroll];
    
    scroll.contentSize = CGSizeMake(scroll.width * suits_.count, scroll.height);
    scroll.pagingEnabled = YES;
     */
#endif
    [[NSNotificationCenter defaultCenter]postNotificationName:@"dismissPopover" object:nil];
}
#pragma mark 刷新配装页面
-(void)suitrefresh:(NSNotification *)sender
{
    NSDictionary *dic = sender.object;
    [scroll removeFromSuperview];
    suits_ = [[DataManager sharedManager] GetSuitArrayByCode:[dic objectForKey:@"code"] ColorId:[dic objectForKey:@"color"]];
    codesearch.text = [dic objectForKey:@"code"];
    [self newrefresh];
}

#pragma mark 查询刷新页面
-(void)searchrefresh:(NSNotification *)sender
{
    NSString *str = sender.object;
    NSArray *arr = [str componentsSeparatedByString:@","];
    [scroll removeFromSuperview];
    suits_ = [[DataManager sharedManager] GetSuitArrayByCode:[arr objectAtIndex:0] ColorId:[arr objectAtIndex:1]];
    [self newrefresh];
    
}

#pragma mark 刷新全部
-(void)totalrefresh:(NSNotification *)sender
{
    [scroll removeFromSuperview];
    suits_ = [[DataManager sharedManager] getSuitArray];

    NSMutableArray *res = [NSMutableArray array];
    if (seriesName_ && seriesName_.length>0) {
        for (SuitObject *obj in suits_) {
            if ([obj.theme isEqualToString:seriesName_]) {
                [res addObject:obj];
            }
        }
        
        suits_ = [NSArray arrayWithArray:res];
    }else{
        
    }
    
    
    
    [self newrefresh];
}

-(void)filterRefresh{
    [scroll removeFromSuperview];
    suits_ = [[DataManager sharedManager] getSuitArrayByFilter:KeyFilter insuit:YES];
    
    NSMutableArray *res = [NSMutableArray array];
    if (seriesName_ && seriesName_.length>0) {
        for (SuitObject *obj in suits_) {
            if ([obj.theme isEqualToString:seriesName_]) {
                [res addObject:obj];
            }
        }
        suits_ = [NSArray arrayWithArray:res];
    }else{
        
    }
    
    
    
    [self newrefresh];
}

-(void)keyClicked:(UIButton *)sender
{
    int category = sender.tag-200;
    
    //先获取点击的按钮的当前过滤条件，以此得到父ID
    KeyObject* key = [KeyFilter objectAtIndex:category-1];
    
    //若父ID为0则表示无上级目录，直接取这个过滤条件的所有项
    NSArray* arr;
    if (key.parentCode==0) {
        arr = [[DataManager sharedManager]getKeyObjects:category];
    }
    else{
        //有上次则根据上次获取该父亲下的所有子目录
        arr = [[DataManager sharedManager]getChildrenKeyObjects:key.parentCode];
    }
    //the controller we want to present as a popover
    controller = [[ShopViewController alloc] initWithStyle:UITableViewStylePlain items:arr tag:sender.tag];
    
    FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
    popover.distance = distance;
    //popover.arrowDirection = FPPopoverArrowDirectionAny;
    popover.tint = FPPopoverDefaultTint;
    
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        popover.contentSize = CGSizeMake(300, 500);
    }
    popover.arrowDirection = FPPopoverArrowDirectionUp;
    
    //sender is the UIButton view
    sender.left -=distance;
    [popover presentPopoverFromView:sender];
    sender.left +=distance;
}

#pragma mark 点击过滤条件刷新页面
-(void)suitFilterRefresh:(NSNotification *)sender
{
    
    NSDictionary* dic =  sender.object;
    
    KeyObject* key = [dic objectForKey:@"key"];
    
    [KeyFilter replaceObjectAtIndex:[[dic objectForKey:@"category"]intValue]-201 withObject:key];
    
    [self updateKeyButton:[[dic objectForKey:@"category"]intValue]-200];
    
    [self filterRefresh];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"dismissPopover" object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

-(void)ret
{
    refreshView.delegate = nil;
    [scroll removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 点击产品进入细节页面
- (void)toDetail:(NSNotification *)notification{
    ProductObject *obj = (ProductObject *)notification.object;
    
    if (obj) {
        ProductDetailViewControllerEx *detail = [[ProductDetailViewControllerEx alloc] initWithProduct:obj];
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    
    
}

#pragma mark 点击poperview框进入细节页面
- (void)toDetail1:(NSNotification *)notification{
    SubSuitObject *sub = notification.object;
    ProductObject *obj = [[DataManager sharedManager] getProduct:sub.productCode withColorID:sub.colorid];
    if (obj) {
        ProductDetailViewControllerEx *detail = [[ProductDetailViewControllerEx alloc] initWithProduct:obj];
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

- (void)dealloc{
    [imageCache_ cancelOperations];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)updatePage:(UIScrollView *)scrollview{
    /*
    int page = scrollview.contentOffset.x/scrollview.width;
//    int idx = 0;
//    for (SuitView *suit in suitView_) {
//        if (idx==page) {
//            
//            [suit loadImage];
//        }else{
//            [suit unloadImage];
//        }
//        idx ++;
//    }
    
    for (int i = 0; i < suits_.count; i++) {
        SuitView *suit = (SuitView *)[scroll viewWithTag:10000+i];
        if (i==page) {
            if (1) {
                
                for (int j = -1; j < 2; j++) {

                    
                    if ((i+j)<0 || (i+j)>=suits_.count) {
                        continue;
                    }
                    
                    suit = (SuitView *)[scroll viewWithTag:10000+i+j];
                    if (suit) {
                        continue;
                    }
                    
                    SuitObject *obj = [suits_ objectAtIndex:i+j];
                    SuitView *s = [[SuitView alloc] initWithSuitObject:obj frame:CGRectMake(0, 0, 320, scroll.height)];
                    [scroll addSubview:s];
                    s.center = CGPointMake(scroll.width/2 + scroll.width * (i+j), scroll.height/2);
                    cWidth = s.right;
                    s.tag = 10000+i+j;
                    
                    [s unloadImage];
                }
            }
            suit = (SuitView *)[scroll viewWithTag:10000+i];
            [suit loadImage];
        }else{
            
            if (i<page-1||i>page+1) {
                SuitView *suit = (SuitView *)[scroll viewWithTag:10000+i];
                if (suit) {
                    [suit removeFromSuperview];
                }

            }
            else if(i==page-1||i==page+1)
            {
                SuitView *suit = (SuitView *)[scroll viewWithTag:10000+i];
                suit.contentOffset = CGPointMake(0, 0);
            }
            
            [suit unloadImage];
        }
    }
     */
}

- (void)clearSuitView{
    for (int i = 0; i < suits_.count; i++) {
        SuitView *suit = (SuitView *)[scroll viewWithTag:10000+i];
        [suit removeFromSuperview];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (![textField.text isEqualToString:@""]) {
        [items removeAllObjects];
        items = [NSMutableArray array];
        searchproduct = [[DataManager sharedManager] SearchSuitArrayByCode:codesearch.text];
        for (int i = 0; i<searchproduct.count; i++) {
            SuitObject *obj1 = [searchproduct objectAtIndex:i];
            SubSuitObject *subobj = [obj1.components objectAtIndex:0];
            NSString *str = [NSString stringWithFormat:@"%@,%@",subobj.productCode,subobj.colorid];
            if ([subobj.productCode isEqualToString:@""]&&[subobj.colorid isEqualToString:@""]) {
                
            }
            else
            {
                [items addObject:str];
            }
        }
        
        
        controller = [[ShopViewController alloc] initWithStyle:UITableViewStylePlain items:items tag:12];
        
        FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:controller];
        
        //popover.arrowDirection = FPPopoverArrowDirectionAny;
        popover.tint = FPPopoverDefaultTint;
        
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            popover.contentSize = CGSizeMake(300, 500);
        }
        popover.arrowDirection = FPPopoverArrowDirectionUp;
        
        //sender is the UIButton view
        [popover presentPopoverFromView:codesearch];
    }
    else
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"totalrefresh" object:nil];
    }
    return YES;
}

-(void)handoff:(UIButton *)sender
{
    [scroll removeFromSuperview];
    [view removeFromSuperview];
    if (isclick == YES) {
        selectedlist = 0;
        [handoff_ setTitle:@"陈列柜" forState:UIControlStateNormal];
        //设置筛选条件
        for ( int i = 0; i < keyCategories_.count; i++ ) {
            KeyObject* key = [KeyFilter objectAtIndex:i];
            
            UIButton* btn = (UIButton*)[filterBtns_ objectAtIndex:key.categoryCode-1];
            
            [btn setHidden:NO];
        }
        
        [codesearch setHidden:NO];
        UIImageView* imgV = (UIImageView*)[tint viewWithTag:1];
        [imgV setHidden:NO];
        page = 0;
        num = 20;
        int maxCol = 5;
        float xOffset = 50;
        float yOffset = 30;
        if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
        {
            scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+tint.bottom, 1024,748-44-64)];
        }
        else
        {
            scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+tint.bottom, 1024,748-44-54)];
        }
        scroll.backgroundColor = [UIColor clearColor];
        scroll.delegate = self;
        [self.view addSubview:scroll];
        
        float height = 0;
        
        for (int i = 0; i < num && i<suits_.count; i++) {
            //        if (style) {
            //            product = [[DataManager sharedManager] getProduct:nil type:pserial style:style page:page pageSize:20];
            //        }
            //        else
            //        {
            //            product = [[DataManager sharedManager] getProduct:nil type:pserial page:page pageSize:20];
            //        }
            
            
            SuitObject *obj = [suits_ objectAtIndex:i];
            SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0 type:SuitView_obj];
            // v.tag = 100+i;
            //        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pThumbURL];
            //        UIImage *img = [imageCache_ imageForKey:obj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
            //        v.imageview.image = img;
            v.tag = 100+i;
            NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
            NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
            UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
            if ([[PMGlobal getInstance]isImageEnable]) {
                if (img) {
                    v.imageview.image = img;
                }else{
                    v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
                }
            }else{
                v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }

            [scroll addSubview:v];
            
            int row = i / maxCol;
            int col = i % maxCol;
            
            v.left = xOffset + col * (v.width + 20);
            v.top = yOffset + row * (v.height + 20);
            
            height = v.bottom;
            
        }
        
        scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
        
        refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectMake(0, 0, 1024, 0)];
        refreshView.delegate = self;
        refreshView.top = scroll.bottom;
        
        [scroll addSubview:refreshView];
        [self setRefreshView];
        
//        
//        float cWidth = 0;
//        
//        suitView_ = [NSMutableArray array];
//        for (int i = 0; i < suits_.count; i++) {
//            SuitObject *obj = [suits_ objectAtIndex:i];
//            SuitView *s = [[SuitView alloc] initWithSuitObject:obj];
//            
//            [suitView_ addObject:s];
//            
//            [scroll addSubview:s];
//            s.center = CGPointMake(scroll.width/2 + scroll.width * i, scroll.height/2);
//            cWidth = s.right;
//        }
        
      //  [self updatePage:scroll];
        
        scroll.contentSize = CGSizeMake(scroll.width * suits_.count, scroll.height);
        scroll.pagingEnabled = YES;
        isclick = NO;
        
        
        NSString *title = [NSString stringWithFormat:@"重点搭配"];
        [self setNavTitle:title];
    }
    else
    {
        [handoff_ setTitle:@"配装" forState:UIControlStateNormal];
        float offset = 35;
        scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(offset, 64, 1024-offset*2, 620)];
        scroll.showsHorizontalScrollIndicator = NO;
        scroll.showsVerticalScrollIndicator = NO;
        scroll.pagingEnabled = YES;
        [self.view addSubview:scroll];
        scroll.top = tint.bottom+0;
        scroll.backgroundColor = [UIColor clearColor];
      //  scroll.center = CGPointMake(1024/2, (748-44)/2);
        scroll.clipsToBounds = YES;
        scroll.delegate = self;
        isclick = YES;
        
        for ( int i = 0; i < keyCategories_.count; i++ ) {
            KeyObject* key = [KeyFilter objectAtIndex:i];
            
            UIButton* btn = (UIButton*)[filterBtns_ objectAtIndex:key.categoryCode-1];
            
            [btn setHidden:YES];
        }
        
        [codesearch setHidden:YES];
        UIImageView* imgV = (UIImageView*)[tint viewWithTag:1];
        [imgV setHidden:YES];
        
                
//        UIImageView *imageview = [[UIImageView alloc]initWithImage:[[UIHelper getInstance] getBundleImage:@"b1bg.jpg"]];
//        imageview.frame = CGRectMake(0, 0, [[UIHelper getInstance] getBundleImage:@"b1bg.jpg"].size.width, [[UIHelper getInstance] getBundleImage:@"b1bg.jpg"].size.height);
//        imageview.clipsToBounds = YES;
//        [scroll addSubview:imageview];
//        scroll.contentSize = CGSizeMake([[UIHelper getInstance] getBundleImage:@"b1bg.jpg"].size.width, [[UIHelper getInstance] getBundleImage:@"b1bg.jpg"].size.height);
        scroll.contentOffset = CGPointMake(100, 100);
        
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1024, 100)];
        view.top = scroll.bottom;
        [self.view addSubview:view];
        
        selectedlist = 1;
        NSString *title = [NSString stringWithFormat:@"重点搭配(%d/%d)",1,[ListImg count]];
        [self setNavTitle:title];
        UIImage *img = [[UIHelper getInstance] getBundleImage:@"anniu.png"];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:img forState:UIControlStateNormal];
        btn.frame = CGRectMake(100, 10, img.size.width, img.size.height);
        [btn setTitle:@"商务生活" forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        btn.tag = 10;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn1 setBackgroundImage:img forState:UIControlStateNormal];
        btn1.frame = CGRectMake(450, 10, img.size.width, img.size.height);
        [btn1 setTitle:@"时尚休闲" forState:UIControlStateNormal];
        btn1.titleLabel.font = [UIFont systemFontOfSize:12];
        btn1.tag = 11;
        [btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn1];

        
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn2 setBackgroundImage:img forState:UIControlStateNormal];
        btn2.frame = CGRectMake(800, 10, img.size.width, img.size.height);
        [btn2 setTitle:@"正装&婚庆" forState:UIControlStateNormal];
        btn2.titleLabel.font = [UIFont systemFontOfSize:12];
        btn2.tag = 12;
        [btn2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn2];
        
        for (int i = 0; i<32; i++) {
            UIImageView *imageview = [[UIImageView alloc]init];
            imageview.frame = CGRectMake(scroll.width*i, 0, scroll.width, scroll.height);
            imageview.tag = 100+i;
            imageview.userInteractionEnabled = YES;
            imageview.clipsToBounds = YES;
            imageview.contentMode = UIViewContentModeScaleAspectFit;
            [scroll addSubview:imageview];
            scroll.contentSize = CGSizeMake(scroll.width*(i+1), 600);
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [imageview addGestureRecognizer:tapGes];
            
            [ListImg addObject:[NSNumber numberWithInt:100+i]];
        }
        
        [self listPicupdatePage:scroll];

    }
}


- (void)listPicupdatePage:(UIScrollView *)scrollview{
    int page1 = scrollview.contentOffset.x/scrollview.width;
    
    int idx = 0;
    for (NSNumber* tag in ListImg) {
        int ii = [tag intValue];
        ii++;
        
        if (idx==page1) {
            [self loadlistImage:scrollview LoadPage:idx];
        }else{
            [self unloadlistImage:scrollview UnloadPage:idx];
        }
        
        idx ++;
    }
    
    NSString *title = [NSString stringWithFormat:@"重点搭配(%d/%d)",page1+1,[ListImg count]];
    [self setNavTitle:title];
}

-(void)loadlistImage:(UIScrollView *)scrollview LoadPage:(int)idx{
    
    UIImage* img;
    if (selectedlist==1) {
//        NSString* name = [NSString stringWithFormat:@"swshuo-0%d.JPG",idx+1];
        
        
        if ([[PMGlobal getInstance]isImageEnable]) {

            img = [[UIHelper getInstance]getBundleImage:[NSString stringWithFormat:@"manual/dapei%02d.jpg",idx]];
        }else{
            img = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }
        
        
    }
//    else if (selectedlist==2) {
//        img = [[UIHelper getInstance]getBundleImage:[NSString stringWithFormat:@"shishang-%02d.JPG",idx+1]];
//    }
//    else if (selectedlist==3) {
//        img = [[UIHelper getInstance]getBundleImage:[NSString stringWithFormat:@"zhengzhuang-%02d.JPG",idx+1]];
//    }
//    
    UIImageView* imgV = (UIImageView*)[scroll viewWithTag:selectedlist*100+idx];
    imgV.contentMode = UIViewContentModeScaleAspectFill;
    imgV.image = img;
    
}

-(void)unloadlistImage:(UIScrollView *)scrollview UnloadPage:(int)idx{

    UIImageView* imgV = (UIImageView*)[scroll viewWithTag:selectedlist*100+idx];
    imgV.image = nil;
}


-(void)click:(UIButton *)sender
{
    [scroll removeFromSuperview];
    float offset = 35;
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(offset, 64, 1024-offset*2, 600)];
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scroll];
    scroll.top = tint.bottom+10;
    scroll.backgroundColor = [UIColor clearColor];
    //  scroll.center = CGPointMake(1024/2, (748-44)/2);
    scroll.clipsToBounds = YES;
    scroll.pagingEnabled = YES;
    scroll.delegate = self;
    [ListImg removeAllObjects];
    if (sender.tag == 10) {
        
        for (int i = 0; i<11; i++) {
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:scroll.bounds];
            imageview.frame = CGRectMake(scroll.width*i, 0, scroll.width, scroll.height);
            imageview.tag = 100+i;
            imageview.userInteractionEnabled = YES;
            imageview.clipsToBounds = YES;
            [scroll addSubview:imageview];
            scroll.contentSize = CGSizeMake(scroll.width*(i+1), 600);
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [imageview addGestureRecognizer:tapGes];
            
            [ListImg addObject:[NSNumber numberWithInt:100+i]];
            selectedlist  = 1;
        }
        
//        UIImageView* first = (UIImageView*) [scroll viewWithTag:100];
//        UIImage * img = [[UIHelper getInstance]getBundleImage:@"商务生活 01.jpg"];
//        first.image = img;
        
    }
    else if (sender.tag == 11) {
        for (int i = 0; i<9; i++) {
            
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:scroll.bounds];
            imageview.frame = CGRectMake(scroll.width*i, 0, scroll.width, scroll.height);
            imageview.tag = 200+i;
            imageview.userInteractionEnabled = YES;
            imageview.clipsToBounds = YES;
            [scroll addSubview:imageview];
            scroll.contentSize = CGSizeMake(scroll.width*(i+1), 600);
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [imageview addGestureRecognizer:tapGes];
            
            [ListImg addObject:[NSNumber numberWithInt:200+i]];
            selectedlist = 2;
        }
//        UIImageView* first = (UIImageView*) [scroll viewWithTag:200];
//        UIImage * img = [[UIHelper getInstance]getBundleImage:@"时尚 01.jpg"];
//        first.image = img;
    }
    if (sender.tag == 12) {
        for (int i = 0; i<7; i++) {
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:scroll.bounds];
            imageview.frame = CGRectMake(scroll.width*i, 0, scroll.width, scroll.height);
            imageview.tag = 300+i;
            imageview.userInteractionEnabled = YES;
            imageview.clipsToBounds = YES;
            [scroll addSubview:imageview];
            scroll.contentSize = CGSizeMake(scroll.width*(i+1), 600);
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [imageview addGestureRecognizer:tapGes];
            [ListImg addObject:[NSNumber numberWithInt:200+i]];
            selectedlist = 3;
        }
        
//        UIImageView* first = (UIImageView*) [scroll viewWithTag:300];
//        UIImage * img = [[UIHelper getInstance]getBundleImage:@"正装 01.jpg"];
//        first.image = img;
    }
    [self listPicupdatePage:scroll];
    
  //  scroll.contentSize = CGSizeMake([[UIHelper getInstance] getBundleImage:@"b1bg.jpg"].size.width, [[UIHelper getInstance] getBundleImage:@"b1bg.jpg"].size.height);
}
- (void)tap:(UITapGestureRecognizer *)sender{
    UIImageView *imageview = (UIImageView *)[scroll viewWithTag:sender.view.tag];
    bigimageview = imageview;
    ImageCropper *cropper = [[ImageCropper alloc] initWithImage:[imageview image]];
	[cropper setDelegate:self];
	
	[self presentModalViewController:cropper animated:YES];
    /*
    NSString * path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pURL];
    NSArray *array = [NSArray arrayWithObject:path];
    PictureViewController *pic = [[PictureViewController alloc] initWithPictures:array withFrame:self.view.frame Desc:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:pic];
    [nav.navigationBar setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController presentModalViewController:nav animated:YES];
    */
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    distance = filterscroll.contentOffset.x;
    [refreshView egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [refreshView egoRefreshScrollViewDidEndDragging:scrollView];
    if (!decelerate) {
        if (selectedlist ==0) {
           [self updatePage:scrollView];
        }
        else{
           [self listPicupdatePage:scrollView];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (selectedlist ==0) {
       [self updatePage:scrollView];
    }
    else{
       [self listPicupdatePage:scrollView];
    }
}

- (void)imageCropper:(ImageCropper *)cropper didFinishCroppingWithImage:(UIImage *)image {
	[bigimageview setImage:image];
	
	[self dismissModalViewControllerAnimated:YES];
	
	[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}

- (void)imageCropperDidCancel:(ImageCropper *)cropper {
	[self dismissModalViewControllerAnimated:YES];
	
	[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
-(void)keyboardWillShow:(NSNotification *)sender
{
    
}
-(void)pop:(NSNotification *)sender
{
    
}

-(void)setRefreshView
{
#ifdef DHH_iOS
    //如果contentsize的高度比表的高度小，那么就需要把刷新视图放在表的bounds的下面
    int height = MAX(scroll.bounds.size.height, scroll.contentSize.height);
    refreshView.frame =CGRectMake(0.0f, height, 320, scroll.bounds.size.height);
#else
    int height = MAX(scroll.bounds.size.height, scroll.contentSize.height);
    refreshView.frame =CGRectMake(0.0f, height, 1024, scroll.bounds.size.height);
#endif
}

#pragma mark - EGORefreshTableFooterDelegate
//出发下拉刷新动作，开始拉取数据
- (void)egoRefreshTableFooterDidTriggerRefresh:(EGORefreshTableFooterView*)view
{
    [self reloadData];
}
//返回当前刷新状态：是否在刷新
- (BOOL)egoRefreshTableFooterDataSourceIsLoading:(EGORefreshTableFooterView*)view
{
    return reloading;
}
//返回刷新时间
-(NSDate *)egoRefreshTableFooterDataSourceLastUpdated:(EGORefreshTableFooterView *)view
{
    return [NSDate date];
}

//请求数据
-(void)reloadData
{
    reloading = YES;
    //新建一个线程来加载数据
    [NSThread detachNewThreadSelector:@selector(requestData)
                             toTarget:self
                           withObject:nil];
}

-(void)requestData
{
    sleep(1);
    //在主线程中刷新UI
    [self performSelectorOnMainThread:@selector(reloadUI)
                           withObject:nil
                        waitUntilDone:NO];
}

-(void)reload
{
#ifdef DHH_iOS
    // [scroll removeFromSuperview];
    [refreshView removeFromSuperview];
    int maxCol = 2;
    float xOffset = 10;
    float yOffset = 20;
    num+=20;
    // scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+[[UIHelper getInstance] getBundleImage:@"detail_tint.png"].size.height, 1024,748-44)];
    // scroll.backgroundColor = [UIColor clearColor];
    // scroll.delegate = self;
    // [self.view addSubview:scroll];
    
    float height = 0;
    page+=1;
    
    if (suits_.count>0) {
        
        for (int i = num-20; i < num && i<suits_.count; i++) {
            //            if (stylestr) {
            //                product = [[DataManager sharedManager] getProduct:nil type:pserialstr style:stylestr page:page pageSize:20];
            //            }
            //            else
            //            {
            //                product = [[DataManager sharedManager] getProduct:nil type:pserialstr page:page pageSize:20];
            //            }
            SuitObject *obj = [suits_ objectAtIndex:i];
            SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0];
            
            //            v.tag = 100+i;
            //            NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pThumbURL];
            //            UIImage *img = [imageCache_ imageForKey:obj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
            //            v.imageview.image = img;
            v.tag = 100+i;
            NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
            NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
            UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
            if (img==nil) {
                v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];
            }
            else
            {
                v.imageview.image = img;
            }

            [scroll addSubview:v];
            
            int row = i / maxCol;
            int col = i % maxCol;
            
            v.left = xOffset + col * (v.width + 10);
            v.top = yOffset + row * (v.height + 10);
            
            height = v.bottom+10;
        }
    }
    else{
        height = scroll.contentSize.height-yOffset;
        [scroll setContentOffset:CGPointMake(scroll.contentOffset.x, scroll.contentOffset.y-70)];
    }
    if (num<product.count) {
        scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
    }
    else
    {
        showMessage(@"没数据了");
        if (isnoobj == NO) {
            if (height!=0) {
                scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
            }
            isnoobj = YES;
        }
        if (scroll.contentSize.height<350) {
            scroll.contentOffset = CGPointMake(0, 0);
        }
        else
        {
            scroll.contentOffset = CGPointMake(0, scroll.contentSize.height-350);
        }

    }
    
    refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectZero];
    refreshView.delegate = self;
    refreshView.top = scroll.bottom;
    
    [scroll addSubview:refreshView];
#else
    // [scroll removeFromSuperview];
    [refreshView removeFromSuperview];
    int maxCol = 5;
    float xOffset = 50;
    float yOffset = 30;
    num+=20;
    // scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10+[[UIHelper getInstance] getBundleImage:@"detail_tint.png"].size.height, 1024,748-44)];
    // scroll.backgroundColor = [UIColor clearColor];
    // scroll.delegate = self;
    // [self.view addSubview:scroll];
    
    float height = 0;
    page+=1;
    
    if (suits_.count>0) {
        
        for (int i = num-20; i < num && i<suits_.count; i++) {
            //            if (stylestr) {
            //                product = [[DataManager sharedManager] getProduct:nil type:pserialstr style:stylestr page:page pageSize:20];
            //            }
            //            else
            //            {
            //                product = [[DataManager sharedManager] getProduct:nil type:pserialstr page:page pageSize:20];
            //            }
            SuitObject *obj = [suits_ objectAtIndex:i];
            SuitThumbView *v = [[SuitThumbView alloc] initWithProduct:obj tag:0 type:SuitView_obj];
            
//            v.tag = 100+i;
//            NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:obj.pThumbURL];
//            UIImage *img = [imageCache_ imageForKey:obj.pThumbURL url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
//            v.imageview.image = img;
            v.tag = 100+i;
            NSString *url = [NSString stringWithFormat:@"_%@",obj.pic_path];
            NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
            UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100];
            if ([[PMGlobal getInstance]isImageEnable]) {
                if (img) {
                    v.imageview.image = img;
                }else{
                    v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
                }
            }else{
                v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
            }

            [scroll addSubview:v];
            
            int row = i / maxCol;
            int col = i % maxCol;
            
            v.left = xOffset + col * (v.width + 20);
            v.top = yOffset + row * (v.height + 20);
            
            height = v.bottom;
        }
    }
    else{
        height = scroll.contentSize.height-yOffset;
        [scroll setContentOffset:CGPointMake(scroll.contentOffset.x, scroll.contentOffset.y-70)];
    }
    if (num<suits_.count) {
        scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
    }
    else
    {
        showMessage(@"没数据了");
        if (isnoobj == NO) {
            if (height!=0) {
                scroll.contentSize = CGSizeMake(scroll.width, height+yOffset);
            }
            isnoobj = YES;
        }
        if (scroll.contentSize.height<710) {
            scroll.contentOffset = CGPointMake(0, 0);
        }
        else
        {
            scroll.contentOffset = CGPointMake(0, scroll.contentSize.height-710);
        }

    }
    
    refreshView = [[EGORefreshTableFooterView alloc]  initWithFrame:CGRectZero];
    refreshView.delegate = self;
    refreshView.top = scroll.bottom;
    
    [scroll addSubview:refreshView];
#endif
}
-(void)reloadUI
{
	reloading = NO;
    
    //停止下拉的动作,恢复表格的便宜
	[refreshView egoRefreshScrollViewDataSourceDidFinishedLoading:scroll];
    //更新界面
    [self reload];
    
    [self setRefreshViewFrame];
}

-(void)setRefreshViewFrame
{
    //如果contentsize的高度比表的高度小，那么就需要把刷新视图放在表的bounds的下面
    int height = MAX(scroll.bounds.size.height, scroll.contentSize.height);
    refreshView.frame =CGRectMake(0.0f, height, self.view.frame.size.width, scroll.bounds.size.height);
}

#pragma mark 进入配装细节页面
//-(void)detail:(NSNotification *)sender
//{
//    SuitObject *suit = sender.object;
//    SuitDetailViewController *detail = [[SuitDetailViewController alloc]initWithNibName:nil bundle:nil obj:suit];
//    [self.navigationController pushViewController:detail animated:YES];
//}

-(void)showtip:(NSNotification *)sender
{
    NSArray *tips = [[DataManager sharedManager]getTipsarray];
    if (tips.count==0) {
        return;
    }
    NSDictionary *tipdic = [tips objectAtIndex:tips.count-1];
    
    [self performSelectorOnMainThread:@selector(addTipView:) withObject:tipdic waitUntilDone:YES];
    
}

- (void)addTipView:(NSDictionary *)dic{
    TipView *tipview = [[TipView alloc]initWithdic:dic];
    [self.navigationController.view addSubview:tipview];
}

- (void)suit_image:(NSNotification *)notification{
    NSDictionary *dict = [notification userInfo];
    UIImage *img = [dict objectForKey:@"image"];
    int tag = [[dict objectForKey:@"tag"] intValue];
    
    SuitThumbView *v = (SuitThumbView *)[scroll viewWithTag:tag];
    
    
    if ([[PMGlobal getInstance]isImageEnable]) {
        if (img) {
            v.imageview.image = img;
        }else{
            v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
        }
    }else{
        v.imageview.image = [[UIHelper getInstance] getBundleImage:@"product_placeholder.jpg"];;
    }
    
}

- (void)setupSeriesName:(NSString *)name{
    seriesName_ = name;
}
@end
